package by.bucha.testproject.content.database.tables;

import android.content.ContentValues;
import by.bucha.testproject.content.bo.workers.LeaderModel;
import by.bucha.testproject.content.bo.workers.SimpleWorkerModel;
import by.bucha.testproject.content.contract.TaskContract;

import com.google.gson.Gson;

public class WorkerTable {

	public WorkerTable() {
	}

	/** The Constant COOKIE_TABLE. */
	public static final String WORKER_TABLE = "WORKER";

	/** The Constant COOKIE_COLUMNS. */
	public static final String[] WORKER_COLUMNS = {
			TaskContract.WorkerColumns.WORKER_ID,
			TaskContract.WorkerColumns.SALARY,
			TaskContract.WorkerColumns.START_TIME_RECEPTION,
			TaskContract.WorkerColumns.END_TIME_RECEPTION,
			TaskContract.WorkerColumns.NAME,
			TaskContract.WorkerColumns.SURNAME,
			TaskContract.WorkerColumns.PATRONYMIC,
			TaskContract.WorkerColumns.LUNCH_TIME,
			TaskContract.WorkerColumns.WORKPLACE_NUMBER,
			TaskContract.WorkerColumns.WORKER_TYPE };

	/** The Constant WORKER_ID_COL_INDEX. */
	public static final int WORKER_ID_COL_INDEX = 0;

	/** The Constant SALARY_COL_INDEX. */
	public static final int SALARY_COL_INDEX = 1;

	/** The Constant START_TIME_RECEPTION_COL_INDEX. */
	public static final int START_TIME_RECEPTION_COL_INDEX = 2;

	/** The Constant END_TIME_RECEPTION_COL_INDEX. */
	public static final int END_TIME_RECEPTION_COL_INDEX = 3;

	/** The Constant NAME_COL_INDEX. */
	public static final int NAME_COL_INDEX = 4;

	/** The Constant SURNAME_COL_INDEX. */
	public static final int SURNAME_COL_INDEX = 5;

	/** The Constant PATRONYMIC_COL_INDEX. */
	public static final int PATRONYMIC_COL_INDEX = 6;

	/** The Constant LAUNCH_TIME_COL_INDEX. */
	public static final int LUNCH_TIME_COL_INDEX = 7;

	/** The Constant WORKPLACE_NUMBER_COL_INDEX. */
	public static final int WORKPLACE_NUMBER_COL_INDEX = 8;

	/** The Constant WORKER_TYPE_COL_INDEX. */
	public static final int WORKER_TYPE_COL_INDEX = 9;

	/** The Constant CREATE_COOKIE_TABLE_SQL. */
	public static final String CREATE_WORKER_TABLE_SQL = "CREATE TABLE "
			+ WORKER_TABLE + " (" + WORKER_COLUMNS[WORKER_ID_COL_INDEX]
			+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ WORKER_COLUMNS[SALARY_COL_INDEX] + " INTEGER, "
			+ WORKER_COLUMNS[START_TIME_RECEPTION_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[END_TIME_RECEPTION_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[NAME_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[SURNAME_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[PATRONYMIC_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[LUNCH_TIME_COL_INDEX] + " TEXT, "
			+ WORKER_COLUMNS[WORKPLACE_NUMBER_COL_INDEX] + " INTEGER, "
			+ WORKER_COLUMNS[WORKER_TYPE_COL_INDEX] + " TEXT)";

	/** The Constant DROP_COOKIE_TABLE_SQL. */
	public static final String DROP_WORKER_TABLE_SQL = "DROP TABLE IF EXISTS "
			+ WORKER_TABLE;

	public static ContentValues getContentValues(String model, String modelType) {
		ContentValues values = null;

		if (modelType.equals(TaskContract.WorkerColumns.LEADER_TABLE_TYPE)) {

			values = getLeaderModelContentValues(model);

		} else if (modelType
				.equals(TaskContract.WorkerColumns.SIMPLE_WORKER_TABLE_TYPE)) {

			values = getSimpleWorkerModelContentValues(model);

		}
		return values;
	}

	private static ContentValues getLeaderModelContentValues(String leader) {

		LeaderModel leaderDeserialised = new Gson().fromJson(leader,
				LeaderModel.class);
		ContentValues values = new ContentValues();

		values.put(WORKER_COLUMNS[SALARY_COL_INDEX],
				leaderDeserialised.getSalary());

		values.put(WORKER_COLUMNS[START_TIME_RECEPTION_COL_INDEX],
				leaderDeserialised.getStartTimeReception().toString());

		values.put(WORKER_COLUMNS[END_TIME_RECEPTION_COL_INDEX],
				leaderDeserialised.getEndTimeReception().toString());

		values.put(WORKER_COLUMNS[NAME_COL_INDEX], leaderDeserialised.getName());

		values.put(WORKER_COLUMNS[SURNAME_COL_INDEX],
				leaderDeserialised.getSurname());

		values.put(WORKER_COLUMNS[PATRONYMIC_COL_INDEX],
				leaderDeserialised.getPatronymic());

		values.put(WORKER_COLUMNS[WORKER_TYPE_COL_INDEX],
				TaskContract.WorkerColumns.LEADER_TABLE_TYPE);

		// it is need to clear simple worker columns
		values.put(WORKER_COLUMNS[LUNCH_TIME_COL_INDEX], "");
		values.put(WORKER_COLUMNS[WORKPLACE_NUMBER_COL_INDEX], "");

		return values;
	}

	private static ContentValues getSimpleWorkerModelContentValues(
			String simpleWorker) {

		SimpleWorkerModel simpleWorkerDeserialised = new Gson().fromJson(
				simpleWorker, SimpleWorkerModel.class);

		ContentValues values = new ContentValues();

		values.put(WORKER_COLUMNS[SALARY_COL_INDEX],
				simpleWorkerDeserialised.getSalary());

		values.put(WORKER_COLUMNS[LUNCH_TIME_COL_INDEX],
				simpleWorkerDeserialised.getLunchTime().toString());

		values.put(WORKER_COLUMNS[WORKPLACE_NUMBER_COL_INDEX],
				simpleWorkerDeserialised.getWorkplaceNumber());

		values.put(WORKER_COLUMNS[NAME_COL_INDEX],
				simpleWorkerDeserialised.getName());

		values.put(WORKER_COLUMNS[SURNAME_COL_INDEX],
				simpleWorkerDeserialised.getSurname());

		values.put(WORKER_COLUMNS[PATRONYMIC_COL_INDEX],
				simpleWorkerDeserialised.getPatronymic());

		values.put(WORKER_COLUMNS[WORKER_TYPE_COL_INDEX],
				TaskContract.WorkerColumns.SIMPLE_WORKER_TABLE_TYPE);

		// it is need to clear leaders columns
		values.put(WORKER_COLUMNS[START_TIME_RECEPTION_COL_INDEX], "");
		values.put(WORKER_COLUMNS[END_TIME_RECEPTION_COL_INDEX], "");

		return values;
	}

}
